﻿using System;
using System.Collections.Generic;
using UnityEngine;

namespace IQIGame.Onigao.Framework
{
    public partial class FrameworkEntry : MonoBehaviour
    {
        private static Dictionary<Type, FrameMonoModule> s_TypeMonoMapField;
        private static Dictionary<Type, FrameMonoModule> s_TypeMonoMap
        {
            get
            {
                if (s_TypeMonoMapField == null)
                {
                    s_TypeMonoMapField = new Dictionary<Type, FrameMonoModule>();
                }
                return s_TypeMonoMapField;
            }
        }

        public static void RegisterModule(FrameMonoModule module)
        {
            s_TypeMonoMap[module.GetType()] = module;
        }

        public static T GetModule<T>() where T : FrameMonoModule
        {
            FrameMonoModule module = null;
            if (s_TypeMonoMap.TryGetValue(typeof(T), out module))
            {
                return (T)module;
            }
            throw new Exception($"GameEntry Get Module Error. Module [{typeof(T).GetType()}] is not existed.");
        }
    }
}
